﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ColorRichTextBox.ColorRichTextBox;

namespace GAM_Web_Designer_Pro.Forms {
    public partial class NewFileTemplateForm : Form {

        private SortedList<string, clsColorRichTextBoxSchema> schemas;
        private clsColorRichTextBox coloredRTB;

        public NewFileTemplateForm() {
            InitializeComponent();
            try {
                schemas = SchemaAccess.LoadAllSchemas();
                _schemaCB.Items.Clear();
                foreach ( clsColorRichTextBoxSchema schema in schemas.Values ) {
                    _schemaCB.Items.Add( schema.SchemeType );
                }
                _schemaCB.SelectedText = "HTML";
            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
        }

        private void _createButton_Click( object sender, EventArgs e ) {
            try {
                if ( !_nameTB.Text.Trim().Equals( string.Empty ) && !_versionTB.Text.Trim().Equals( String.Empty )
                        && _schemaCB.SelectedIndex >= 0 ) {
                    Template temp = new Template();
                    temp.Name = _nameTB.Text.Trim();
                    temp.Version = _versionTB.Text.Trim();
                    temp.Language = schemas.Values[ schemas.IndexOfKey(_schemaCB.Text) ].SchemeType.ToString();
                    temp.Text = _textRTB.Text.Trim();
                    temp.Key = "GAM_TEMPLATE_KEY[" + temp.Name + "," + temp.Version + "]";
                    TemplateAccess.SaveTemplate( temp );
                    MessageBox.Show( "Template Created.", "", MessageBoxButtons.OK );
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
        }

        private void _cancelButton_Click( object sender, EventArgs e ) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void _schemaCB_SelectedIndexChanged( object sender, EventArgs e ) {
            try {
                if ( _schemaCB.SelectedIndex >= 0 ) {
                    clsColorRichTextBoxSchema schema = new clsColorRichTextBoxSchema( schemas.Values[ _schemaCB.SelectedIndex ] );
                    coloredRTB = new clsColorRichTextBox( _textRTB, schema, true );
                    coloredRTB.UnColorEntireText();
                    coloredRTB.RecolorEntireText();
                }
            }
            catch ( Exception ex ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                  Application.ProductVersion, ex.ToString(), ex.Message ).Show();
            }
        }

        private void NewFileTemplateForm_Load( object sender, EventArgs e ) {
            
        }
    }
}
